package com.ruoyi.web.ydz.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.ydz.domain.News;
import com.ruoyi.web.ydz.domain.OtherNewsDisease;
import com.ruoyi.web.ydz.service.IOtherNewsDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.ydz.mapper.OtherNewsMapper;
import com.ruoyi.web.ydz.domain.OtherNews;
import com.ruoyi.web.ydz.service.IOtherNewsService;

/**
 * 其他新闻Service业务层处理
 * 
 * @author Drx
 * @date 2025-08-14
 */
@Service
public class OtherNewsServiceImpl implements IOtherNewsService 
{
    @Autowired
    private OtherNewsMapper otherNewsMapper;

    @Autowired
    private IOtherNewsDiseaseService otherNewsDiseaseService;


    /**
     * 查询其他新闻
     * 
     * @param id 其他新闻主键
     * @return 其他新闻
     */
    @Override
    public OtherNews selectOtherNewsById(Long id)
    {
        OtherNews otherNews = otherNewsMapper.selectOtherNewsById(id);
        if(null != otherNews){
            OtherNewsDisease otherNewsDisease = new OtherNewsDisease();
            otherNewsDisease.setNewsId(id);
            List<OtherNewsDisease> list = otherNewsDiseaseService.selectOtherNewsDiseaseList(otherNewsDisease);
            otherNews.setOtherNewsDiseaseList(list);
        }
        return otherNews;
    }

    /**
     * 查询其他新闻列表
     * 
     * @param otherNews 其他新闻
     * @return 其他新闻
     */
    @Override
    public List<OtherNews> selectOtherNewsList(OtherNews otherNews)
    {
        return otherNewsMapper.selectOtherNewsList(otherNews);
    }

    /**
     * 新增其他新闻
     * 
     * @param otherNews 其他新闻
     * @return 结果
     */
    @Override
    public int insertOtherNews(OtherNews otherNews)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        otherNews.setCreateBy(user.getUserName());
        otherNews.setCreateName(user.getNickName());
        otherNews.setCreateTime(DateUtils.getNowDate());

        otherNews.setUpdateBy(user.getUserName());
        otherNews.setUpdateName(user.getNickName());
        otherNews.setUpdateTime(DateUtils.getNowDate());
        int i = otherNewsMapper.insertOtherNews(otherNews);
        if(otherNews.getOtherNewsDiseaseList().size()>0) {
            otherNews.getOtherNewsDiseaseList().forEach(item->{
                item.setNewsId(otherNews.getId());
                otherNewsDiseaseService.insertOtherNewsDisease(item);
            });
        }
        return i;
    }

    /**
     * 修改其他新闻
     * 
     * @param otherNews 其他新闻
     * @return 结果
     */
    @Override
    public int updateOtherNews(OtherNews otherNews)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        otherNews.setUpdateBy(user.getNickName());
        otherNews.setUpdateName(user.getUserName());
        otherNews.setUpdateTime(DateUtils.getNowDate());
        //删除疾病关联信息
        if(null != otherNews.getDelOtherNewsDiseaseIds() && otherNews.getDelOtherNewsDiseaseIds().length>0){
            otherNewsDiseaseService.deleteOtherNewsDiseaseByIds(otherNews.getDelOtherNewsDiseaseIds());
        }
        if(otherNews.getOtherNewsDiseaseList().size()>0) {
            otherNews.getOtherNewsDiseaseList().forEach(item->{
                if (Objects.isNull(item.getId())){
                    item.setNewsId(otherNews.getId());
                    otherNewsDiseaseService.insertOtherNewsDisease(item);
                }else {
                    otherNewsDiseaseService.updateOtherNewsDisease(item);
                }

            });
        }
        return otherNewsMapper.updateOtherNews(otherNews);
    }

    /**
     * 批量删除其他新闻
     * 
     * @param ids 需要删除的其他新闻主键
     * @return 结果
     */
    @Override
    public int deleteOtherNewsByIds(Long[] ids)
    {
        return otherNewsMapper.deleteOtherNewsByIds(ids);
    }

    /**
     * 删除其他新闻信息
     * 
     * @param id 其他新闻主键
     * @return 结果
     */
    @Override
    public int deleteOtherNewsById(Long id)
    {
        return otherNewsMapper.deleteOtherNewsById(id);
    }

    @Override
    public AjaxResult selectOtherNewsByLabel(OtherNews otherNews) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<OtherNews> otherNewsList = otherNewsMapper.selectOtherNewsByLabel(otherNews);
            ajaxResult.put("data", otherNewsList);
            ajaxResult.put("code", 200);
            ajaxResult.put("msg", "查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.put("code", 500);
            ajaxResult.put("msg", "获取新闻失败，请联系管理员！");
        }
        return ajaxResult;
    }
    /**
     * 增加浏览量
     *
     * @param id
     * @return 结果
     */
    public void updateOtherNewsHis(Long id){
        otherNewsMapper.updateOtherNewsHis(id);
    }
}
